package com.alibaba.algo.util;

/**
 * Created by IntelliJ IDEA.
 * User: glorysongglory
 * Date: 2020/4/15
 * Time: 12:41
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类.
 */
public class DateUtil {
    /**
     * 日期格式
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 日期时间格式
     */
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期时间格式Full
     */
    public static final String FULL_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    /**
     * 日期格式
     */
    public static final String DEFAULT_DATETIME_T_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * 时间格式
     */
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    /**
     * 年月日时分
     */
    public static final String DEFAULT_TIME_HM_FORMAT = "yyyy-MM-dd HH:mm";

    /**
     * 例如: 2017-09-01 00:00:00 到 2017-09-03 23:59:59的毫秒数
     */
    public final static long threedayTime = 259199000l;

    /**
     * 将<tt>Date</tt>转换成<tt>String</tt>,使用年月日小时分钟秒格式.
     *
     * @param date yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String format(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DateUtil.DEFAULT_DATETIME_FORMAT);
        return format.format(date);
    }

    public static String format(Date date, String formar) {
        SimpleDateFormat format = new SimpleDateFormat(formar);
        return format.format(date);
    }

    /**
     * 将<tt>String</tt>转换成<tt>Date</tt>,使用年月日小时分钟秒格式.
     *
     * @param source
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static Date parse(String source) {
        SimpleDateFormat format = new SimpleDateFormat(DateUtil.DEFAULT_DATETIME_FORMAT);
        try {
            return format.parse(source);
        } catch (ParseException e) {
            format = new SimpleDateFormat(DateUtil.DEFAULT_DATE_FORMAT);
            ;
            try {
                return format.parse(source);
            } catch (ParseException e1) {
                return null;
            }
        }
    }

    /**
     * 将<tt>String</tt>转换成<tt>Date</tt>,使用指定格式. 如果转换异常,返回null.
     *
     * @param source
     * @param formar
     * @return
     */
    public static Date parse(String source, String formar) {
        SimpleDateFormat format = new SimpleDateFormat(formar);
        try {
            return format.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将日期增加/减少指定天数.
     *
     * @param date
     * @return
     */
    public static Date addDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, day);
        return cal.getTime();
    }

    /**
     * 将日期增加/减少指定小时.
     *
     * @param date
     * @return
     */
    public static Date addHour(Date date, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);
        return cal.getTime();
    }

    /**
     * 将日期增加/减少指定分钟.
     *
     * @param date
     * @return
     */
    public static Date addMinute(Date date, int minute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);
        return cal.getTime();
    }

    /**
     * 将日期增加/减少指定秒.
     *
     * @param date
     * @return
     */
    public static Date addSecond(Date date, int second) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, second);
        return cal.getTime();
    }

    /**
     * 以友好的方式显示时间信息.
     *
     * @param time
     * @return
     */
    public static String friendlyDate(Date time) {
        if (time == null)
            return "未知";

        int ct = (int) ((System.currentTimeMillis() - time.getTime()) / 1000);

        if (ct < 3600) {
            return Math.max(ct / 60, 1) + "分钟以前";
        }
        if (ct >= 3600 && ct < 86400) {
            return (ct / 3600) + "小时以前";
        }
        if (ct >= 86400 && ct < 2592000) { // 86400 * 30
            int day = ct / 86400;
            if (day > 1) {
                return day + "天以前";
            }
            return "昨天";
        }
        if (ct >= 2592000 && ct < 31104000) { // 86400 * 30
            return (ct / 2592000) + "月以前";
        }
        return (ct / 31104000) + "年以前";
    }

    /**
     * @return 返回当前年
     */
    public static int getYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * @return 返回当前月
     */
    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * @return 返回当前天
     */
    public static int getDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    /**
     * @return 返回当前小时
     */
    public static int get24Hour() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * @return 返回当前分钟
     */
    public static int getMinute() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * @return 返回当前秒
     */
    public static int getSecond() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.SECOND);
    }

    /**
     * @return 返回当前时间星期几.
     */
    public static int getWeek() {
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        return (week == 1) ? 7 : week - 1;
    }

    public static long getMillis(String date) {
        return DateUtil.parse(date).getTime();
    }

    public static Date randomDate(String beginDate) {
        return randomDate(beginDate, DateUtil.format(new Date()));
    }

    public static Date randomDate(String beginDate, String endDate) {
        long start = DateUtil.getMillis(beginDate);
        long end = DateUtil.getMillis(endDate);

        if (start >= end) {
            return null;
        }
        long date = randomTime(start, end);
        return new Date(date);
    }

    private static long randomTime(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return randomTime(begin, end);
        }
        return rtn;
    }

    public static Date getToday(int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 获取时间戳
     *
     * @return
     */
    public static long getTimeStamp() {
        return (new Date().getTime() / 1000);
    }

    public static Date addMonth(Date startDate, int addMonth) {
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.add(Calendar.MONTH, addMonth);
        return c.getTime();
    }
}


